package com.mi.million.module.system.controller;

import com.alibaba.fastjson.JSON;
import com.mi.million.module.system.mapper.SysMapper;
import com.mi.million.module.system.service.DictService;
import com.mi.million.module.system.service.SystemService;
import com.mi.million.mybatis.entity.SysDict;
import com.mi.rule.auth.Public;
import com.mi.rule.db.dao.MiDao;
import com.mi.rule.resource.ResC;
import com.mi.rule.resource.ResM;
import com.mi.rule.util.project.SRS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@ResC(value = "字典管理", parent = "系统配置")
@RequestMapping("/system/dict")
public class DictController {
    @Autowired
    SystemService systemService;
    @Autowired
    DictService dictService;
    @Autowired
    MiDao miDao;
    @Autowired
    SysMapper sysMapper;

    // 主从库测试-只支持单数据源事务
//    @Public
//    @ResM("主从-读写-测试")
//    @RequestMapping({"/master/slave"})
//    public SRS masterSlave() {
//        SysDict sysDict = new SysDict();
//        sysDict.setId("200");
//        sysDict.setValue("主库200");
//        long update = miDao.update(sysDict);
//        SysDict sysDict1 = miDao.selectById(SysDict.class, 200);

//        SRS srs0 = systemService.test0();
//        SRS srs1 = systemService.test2("564");
//        SRS srs2 = systemService.test3("564");
//        System.out.println(srs1.toJSONString());
//        System.out.println(srs2.toJSONString());
//        SRS srs = systemService.test1("564");
//        System.out.println(srs.toJSONString());
//        return SRS.bySuccess();
//    }

    // 摘除或换位置
    @Public
    @ResM("前端字典信息")
    @RequestMapping({"/manage/dictList"})
    public SRS dictList() {
        //    public SRS dictList(@RequestBody @Valid SysDict sysDict, BindingResult bindingResult) {
        //        checkParam(bindingResult);
        return SRS.bySuccess(
                SRS.create()
                        .set("companyType", JSON.parseArray(dictService.getValue("company_type")))
                        .set("companyScale", JSON.parseArray(dictService.getValue("company_scale")))
                        .set("peopleType", JSON.parseArray(dictService.getValue("people_type")))
                        .set("peopleAge", JSON.parseArray(dictService.getValue("people_age")))
                        .set("politicsFace", JSON.parseArray(dictService.getValue("politics_face")))
                        .set("unitType", JSON.parseArray(dictService.getValue("unit_type")))
                        .set("jobType", JSON.parseArray(dictService.getValue("job_type")))
                        .set("learnLevel", JSON.parseArray(dictService.getValue("learn_level")))
                        //
                        .set("serverVersion", dictService.getValue("server_version"))
                        .set("adminVersion", dictService.getValue("admin_version"))
                        .set("webSocketUrl", dictService.getValue("web_socket_url"))
                        .set("adminShowLog", dictService.getValue("admin_show_log"))
        );
    }

    @ResM("添加字典")
    @RequestMapping({"/insert"})
    public SRS insert(@RequestBody SysDict dict) {
        return systemService.insertDict(dict);
    }


    @ResM("删除字典")
    @RequestMapping({"/delete"})
    public SRS delete(@RequestBody Object[] ids) {
        return systemService.deleteDict(ids);
    }


    @ResM("修改字典")
    @RequestMapping({"/update"})
    public SRS update(@RequestBody SysDict dict) {
        return systemService.updateDict(dict);
    }


    @ResM("字典分页查询")
    @RequestMapping("/page")
    public SRS page(@RequestBody SRS param) {
        return systemService.pageDict(param);
    }

    @ResM("字典列表查询")
    @RequestMapping({"/list"})
    public SRS list(@RequestBody SRS param) {
        return systemService.listDict(param);
    }

    @ResM("字典详情查询")
    @RequestMapping("/info")
    public SRS one(@RequestBody SRS param) {
        return systemService.infoDict(param);
    }


    @ResM("刷新字典缓存")
    @RequestMapping("/public/refresh/5156D05BCCAE98AC70FD2251D35D401A")
    public SRS one() {
        return systemService.removeDictCache();
    }


}
